<?php
/**
 * Store and read User data
 *
 * @author     Mario Schillermann <info@rockit-flash.com>
 * @encoding   UTF-8 äöüßÖÄÜ
 * @copyright  © 2011 Mario Schillermann (info@rockit-flash.com)
 * @license    GNU GPL v2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 * @uses       
 * @package    User
 * @subpackage Model
 * @todo
 */

class User_Model_User extends Zend_Db_Table_Abstract
{
    /**
     * Get user data
     *
     * @return array user data
     */
    public function read($userid)
    {
        $query = $this->_db->select()
                           ->from(array('u' => 'login_users'),
                                  array('id', 'username', 'firstname', 'lastname'))
                           ->join(array('r' => 'access_roles'),
                                  'u.role = r.id',
                                  array('role'))
                           ->where('u.id' . ' = ?', (integer) $userid);

        return $this->_db->fetchRow($query);
    }

    /**
     * Delete user data
     *
     * @return array user data
     */
    public function delete($userid)
    {
        $this->_db
             ->delete(
                 'login_users',
                 array(
                     'id = ?' => (integer) $userid
                 )
             );
        
    }
    
    /**
     * Get user list
     *
     * @return void
     */
    public function getList()
    {
        $query = $this->_db->select()
                           ->from('login_users',
                                  array('id', 'username'));

        return $this->_db->fetchAll($query);
    }

    /**
     * Stores user data
     * 
     * @param  array  &$formRequest
     * @param  string $userId
     * @return void
     */
    public function save($data, $userId = null)
    {
        if ($userId == null) {
            $this->_db->insert('login_users', $data);
            return $this->_db->lastInsertId();
        } else {
            $where = $this->_db->quoteInto('id = ?', (integer) $userId);
            $this->_db->update('login_users', $data, $where);
        }
    }
}